package edu.jd.movieplatform.security.register.impl;

import edu.jd.movieplatform.security.register.RegisterDto;
import edu.jd.movieplatform.security.register.RegisterService;
import edu.jd.movieplatform.security.register.dao.RegisterDao;
import edu.jd.movieplatform.security.register.Register;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

@Service //说明这是一个受spring容器管理的业务组件(Bean)
@Transactional //说明本类的所有方法都是事务性
public class RegisterServiceImpl implements RegisterService {

    @Resource
    private RegisterDao registerDao;



    @Override
    public void addUser(RegisterDto dto) {
        Register user = getUserById(dto.getCust_username());
        if(user != null){
            throw new RuntimeException("已存在相同账号的用户！");
        }
        if(dto.getPassword()!=dto.getConfirm_password()){
            dto.setUser_status("cust");
            dto.setPassword(dto.getPassword());
            registerDao.insertUsertoCust(dto);
            registerDao.insertUsertoCR(dto);
        }else {
            throw new RuntimeException("两次输入密码不同！");
        }

    }

    @Override
    public Register getUserById(String cust_username) {
        Register register = registerDao.findUserById(cust_username);
        return register;
    }


}
